/*******************************************************************************
 * Copyright 2007, 2009 InnovaSoft SA http://www.innovasoft.biz/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
 
package biz.innovasoft.ndb.index;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;

public class IndexManager implements Serializable {

	private static final long serialVersionUID = -1285170436221245303L;

	private String name;
	private Class<?> clazz;
	private boolean isRootIndex = false;
	private Map<String, IndexEntry> indexEntries = new HashMap<String, IndexEntry>();

	private Directory directory = new RAMDirectory();

	public IndexManager (Class<?> clazz) {
		this(clazz, clazz.getCanonicalName(), false);
	}
	
	public IndexManager (Class<?> clazz, boolean isRootIndex) {
		this(clazz, clazz.getCanonicalName(), isRootIndex);
	}
	
	public IndexManager(Class<?> clazz, String name, boolean isRootIndex) {
		this.name = name;
		this.clazz = clazz;
		this.isRootIndex = isRootIndex;
	}

	public String getName() {
		return name;
	}
	
	public Class<?> getIndexClass() {
		return clazz;
	}
	
	public boolean isRootIndex() {
		return isRootIndex;
	}

	public void addIndexEntry(IndexEntry entry) {
		indexEntries.put(entry.getIndexPath(), entry);
	}

	public IndexEntry getIndexEntry(String indexPath) {
		return indexEntries.get(indexPath);
	}

	public IndexEntry[] getIndexEntries() {
		return (IndexEntry[]) indexEntries.values().toArray(new IndexEntry[0]);
	}

	public boolean containsEntry(String indexPath) {
		return indexEntries.containsKey(indexPath);
	}

	public Directory getDirectory() {
		return directory;
	}
	
	public void initialize () {
		directory = new RAMDirectory();
	}

}
